/*================================================
 * 
 * Createtime : 2016/05/26
 * Author : luo yanming
 * 
 ================================================*/
define(function(require, exports, module) {
	var $ = require('jquery'),
		layer = require('layer'),
		lazyLoad = require('lazyLoad');

	var util = {
		init: function() {
			util.bind();
		},
		bind: function() {
			util.calculateHeight();

			window.onresize = function(){
				util.calculateHeight();
			}
			util.imgLazyLoad();
			util.signInUpBind();
			util.addrSelectBind();
			util.sidebarBind();
		},
		calculateHeight: function() {
			var headerH = $('header').outerHeight(),
				footerH = $('footer').outerHeight(),
				windowH = $(window).outerHeight(),
				processH = $('.process').outerHeight(),
				cH = windowH - headerH - footerH - processH - 40;
	
			$('.main-content').css('min-height', cH);
		},
		signInUpBind: function(){
			var signInBtn = $('header .signin'),
				signUpBtn = $('header .signup'),
				signInUp = $('.sign-in-up'),
				BoxInUp = signInUp.find('.box-in-up'),
				BoxForget = signInUp.find('.box-forget'),
				signInUpTabNavLi = BoxInUp.find('.tab-nav ul li'),
				signInUpTabListLi = BoxInUp.find('.tab-list ul li');

			//头部登录
			signInBtn.on('click', function(){
				BoxInUp.show();
				BoxForget.hide();
				signInUp.fadeIn();
				util.signInUpShow(0);
			});

			//头部注册
			signUpBtn.on('click', function(){
				BoxInUp.show();
				BoxForget.hide();
				signInUp.fadeIn();
				util.signInUpShow(1);
			});

			//关闭弹框
			signInUp.on('click', function(e){
				if(e.target.nodeName == 'SECTION'){
					signInUp.fadeOut();
				}else{
					return false;
				}
			});

			//
			signInUpTabNavLi.on('click', function(e) {
				var _this = $(this);
				if(_this.hasClass('active')) {
					return false;
				} else {
					_this.addClass('active').siblings().removeClass('active');
					util.signInUpShow(_this.index());
				}
			});

			//没有账号？
			signInUpTabListLi.find('.no-account').on('click', function(e) {
				util.signInUpShow(1);
			});

			//忘记密码？
			signInUpTabListLi.find('.forget-pwd').on('click', function(e) {
				BoxInUp.fadeOut();
				BoxForget.fadeIn();
			});

		},
		signInUpShow: function(type) {
			var signNavLi = $('.sign-in-up .box-in-up .tab-nav ul li'),
				signListLi = $('.sign-in-up .box-in-up .tab-list ul li');

			signNavLi.eq(type).addClass('active').siblings().removeClass('active');
			signListLi.eq(type).addClass('active').siblings().removeClass('active');
		},
		addrSelectBind: function() {
			var addrSelect = $('header .addr-select'),
				addrSelectArrow = addrSelect.find('.text i'),
				addrSelectDrop = addrSelect.find('.drop');

			addrSelect.on('mouseenter', function(ev) {
                addrSelectArrow.addClass('active');
                addrSelectDrop.addClass('active');
            });

            addrSelect.on('mouseleave', function(ev) {
                addrSelectArrow.removeClass('active');
                addrSelectDrop.removeClass('active');
            });
		},
		sidebarBind: function() {
			var sidebarLiA = $('.sidebar ul li a'),
				aIndex = [];

			sidebarLiA.mouseenter(function() {
				aIndex = layer.tips($(this).attr('data-tip'), this, {
					tips: [4, '#ff9800']
				});
			}).mouseleave(function() {
				layer.close(aIndex);
			});

			$(".sidebar .backtop").on('click', function() {
                $('body,html').animate({scrollTop:0},600);
                return false;
            });
		},
		imgLazyLoad: function() {
			if($('.lazyload').length == 0){
				return false;
			}
			$('.lazyload').lazyload({
				placeholder: '',
				effect: "fadeIn",
				threshold: 100000,
				container: $('.main-wrapper'),
				failurelimit: 100
			});
		},
		checkSubmitMobile: function(mobile){
			var re = /^1\d{10}$/;
			if (re.test(mobile)) {
				return true;
			} else {
				return false;
			}
		},
		checkNull: function(value){
			var v = value.trim();
			if(v == '' || v == undefined || v == null){
				return false;
			}else{
				return true;
			}
		},
		getQueryString: function(name){
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		},
        cookieVal: function(cookieName) {
        	var thisCookie = document.cookie.split("; ");
        	for (var i=0; i<thisCookie.length; i++) {
        		if (cookieName == thisCookie[i].split("=")[0]) {
        			return thisCookie[i].split("=")[1];
        		}
        	}
        	return 0;
        },
        wechatShareReady: function(title, desc, link, imgUrl){
       		wx.ready(function() {
	            var shareData = {
	                title: title,
	                desc: desc,
	                link: link,
	                imgUrl: imgUrl
	            };
	            wx.onMenuShareAppMessage(shareData);
	            wx.onMenuShareTimeline(shareData);
	            wx.onMenuShareQQ(shareData);
	            wx.onMenuShareWeibo(shareData);
	            wx.onMenuShareQZone(shareData);
	        })
	        wx.error(function(res) {
	            iknow.alert(res.errMsg);
	        });
        },
        setHeaderTitle: function(title){
        	var tbox = $('header h1');
        	tbox.html(title);
        },
        setDocumentTitle: function(title){
        	document.title = title;
        }
	};

	util.init();
	module.exports = util;
});